﻿using BaseUtils.Helper;
using Newtonsoft.Json;
using NWMES.MiAssembleAndCollectDataForSfcServiceService;
using NWMES.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace NWMES.Mes
{
    public class MesMiAssembleAndCollectDataForSfcService : MESBaseService
    {

        public static NetworkCredential networkCredential = null;
        static MesMiAssembleAndCollectDataForSfcService Params = null;
        static string DefaultApiSection = "miAssembleAndCollectDataForSfc";

        static MesMiAssembleAndCollectDataForSfcService()
        {
            Params = config[DefaultApiSection].ToObject<MesMiAssembleAndCollectDataForSfcService>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 模组装配电芯，带收数功能
        /// </summary>
        /// <param name="sfc"></param>
        /// <param name="inventorys"></param>
        /// <param name="parametricDataArray"></param>
        /// <param name="ApiSection"></param>
        /// <returns></returns>
        public static MesResult RequestMes(string sfc,
            List<MachineIntegrationParamData> MesDataInventory, List<MachineIntegrationParamData> MesData,
            string ApiSection = null)
        {
            MiAssembleAndCollectDataForSfcServiceService.MiAssembleAndCollectDataForSfcServiceService Service;
            assembleAndCollectDataForSfcRequest Request = new assembleAndCollectDataForSfcRequest();
            miAssmebleAndCollectDataForSfc DataForSfc = new miAssmebleAndCollectDataForSfc();
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MesMiAssembleAndCollectDataForSfcService>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }

            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }

            SetRequestParams(sfc, out Service, out Request);

            // 装配的数据
            if (MesDataInventory?.Count > 0)
            {
                // 电芯码，数组
                Request.inventoryArray = MesDataInventory.Select(item =>
                {
                    miInventoryData md = new miInventoryData() { inventory = item.Value, qty = item.qty };
                    return md;
                }).ToArray(); 
            }

            // 收数数据
            if (MesData?.Count > 0)
            {
                Request.parametricDataArray = MesData.Select(s =>
                {
                    ParameterDataType dataType = new ParameterDataType();
                    Enum.TryParse(s.DataType.ToString(), out dataType);
                    machineIntegrationParametricData Param = new machineIntegrationParametricData() { name = s.Name, value = s.Value, dataType = dataType };
                    return Param;
                }).ToArray();
            }

            DataForSfc.AssembleAndCollectDataForSfcRequest = Request;

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(DataForSfc));
            miAssmebleAndCollectDataForSfcResponse Response = new miAssmebleAndCollectDataForSfcResponse();

            try
            {
                Response = Service.miAssmebleAndCollectDataForSfc(DataForSfc);
                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }
            SaveMesLog(ApiSection, result);
            return result;
        }

        private static void SetRequestParams(string sfc,
            out MiAssembleAndCollectDataForSfcServiceService.MiAssembleAndCollectDataForSfcServiceService Service,
            out assembleAndCollectDataForSfcRequest Request)
        {
            Service = new MiAssembleAndCollectDataForSfcServiceService.MiAssembleAndCollectDataForSfcServiceService();
            Service.Url = Params.Url;
            Service.Timeout = Params.Timeout;
            Service.Credentials = networkCredential;
            Service.PreAuthenticate = true;

            Request = new assembleAndCollectDataForSfcRequest();
            Request.site = Params.Site;
            Request.user = Params.User;
            Request.operation = Params.Operation;
            Request.operationRevision = Params.OperationRevision;
            Request.activityId = Params.ActivityId;
            Request.resource = Params.Resource;
            Request.dcGroup = Params.DcGroup;
            Request.dcGroupRevision = Params.DcGroupRevision;
            Request.modeProcessSFC = ConvertHelper.ToEnum<MiAssembleAndCollectDataForSfcServiceService.dataCollectForSfcModeProcessSfc>(Params.ModeProcessSFC);
            Request.sfc = sfc;
        }
    }
}
